package com.clei.utils.mybatis;

import com.clei.utils.Base64Util;
import com.clei.utils.EncryptUtil;

import java.nio.charset.StandardCharsets;

/**
 * AES 加密工具
 *
 * @author Y
 * @date 2023-09-17
 */
public class AESDataEncryptor implements DataEncryptor {

    /**
     * key
     */
    private final String aesKey = "abcd0001ABCD0001";

    /**
     * 加密
     *
     * @param origin 原数据
     * @return 加密后的数据
     */
    @Override
    public String encrypt(String origin) {
        byte[] bytes = String.valueOf(origin).getBytes(StandardCharsets.UTF_8);
        byte[] resBytes = EncryptUtil.encryptAES(bytes, aesKey);
        return Base64Util.encode(resBytes);
    }

    /**
     * 解密
     *
     * @param encrypted 加密后的数据
     * @return 原数据
     */
    @Override
    public String decrypt(String encrypted) {
        byte[] bytes = Base64Util.decode(encrypted);
        byte[] resBytes = EncryptUtil.decryptAES(bytes, aesKey);
        return new String(resBytes, StandardCharsets.UTF_8);
    }
}
